<?

$local_connect = $GLOBALS['master_db'];
$dns_dom_id = $GLOBALS['dns_dom_id'];
$dom = dns_dom_settings($dns_dom_id);

if(!$dom){
	exit;
}

$query = "SELECT address FROM dns_cluster.addresses WHERE type='store' ORDER BY used LIMIT 1";
$local_connect->query($query);
$dom_a_record = $local_connect->getNext();
$store_default_ip = $dom_a_record['address'];

if($dom['open_store'] == 't'){
	$dom_a_record = get_domain_records($dns_dom_id, array('r.type'=>'A','r.name'=>$dom['dns_dom_name']));
	$a_record = $dom_a_record[0]['content'];
	$checked = 'checked';
	$disabled = '';
	$params = "'open_store::yes|store_a_record' + document.getElementById('store_a_record').value";
}elseif($dom['open_store'] == 'f'){
	$a_record = $store_default_ip;
	$checked = '';
	$disabled = 'disabled';
	$params = '';
}

$records = getDomainRecords(array('domain_id'=>$dns_dom_id));
$cant_update_types = array('SOA', 'NS');
$cant_update_names = array(
	'mail.'.$dom[dns_dom_name],
	'mx1.'.$dom[dns_dom_name],
	'mx2.'.$dom[dns_dom_name]
	
);
$ns = getDomainRecords(array('domain_id'=>$dns_dom_id, 'type'=>'NS'));
foreach($ns as $k => $v){
        $cant_update_names[] = $v['content'];
}


$recordsTable = "<table class=bTable cellspacing=0 cellspacing=0>
        <tr>
                <th>Hostname</th>
                <th>Type</th>
                <th>Content</th>
		<th>Edit</th>
		<th>Delete</th>
        </tr>";

foreach($records as $k => $v){
	if(in_array($v[type], $cant_update_types) or in_array($v[name], $cant_update_names)){
		$cant_update_flag = true;
		$name_box = $v['name'];
		$content_box = $v['content'];
		$name_val = $v['name'];
		$content_val = $v['content'];
	}else{
		$cant_update_flag = false;
		if($v[type] == 'A'){
			$name_box = "<input type=text name='rname' value='$v[name]' id='rname_$v[id]' maxlength='128'>";
			$name_val = "' + document.getElementById('rname_$v[id]').value  + '";
		}elseif($v[type] == 'MX'){
			$name_box = $v['name'];
			$name_val = $v['name'];
		}
		$content_box = "<input type=text name='rcontent' value='$v[content]' id='rcontent_$v[id]' maxlength='128'>";
		$content_val = "' + document.getElementById('rcontent_$v[id]').value  + '";
	}
        $recordsTable .= "<tr>
       	        <td>$name_box</td>
               	<td>$v[type]</td>
                <td width=40%>$content_box</td>
		<td>".($cant_update_flag ? '-' : "<a href='#'
onClick=\"if(confirmSubmit('".addslashes(translate('confirm_edit_record'))."')){editRecord('dns_dom_id::$dns_dom_id|id::$v[id]|name::$name_val|type::$v[type]|content::$content_val');}return false;\">[edit]</a>")."</td>
		<td>".($cant_update_flag ? '-' : "<a href='#'
onClick=\"if(confirmSubmit('".addslashes(translate('confirm_delete_record'))."')){deleteRecord('dns_dom_id::$dns_dom_id|id::$v[id]');}return false;\">[delete]</a>")."</td>
        </tr>";
}

$recordsTable .= '</table>';

$addTable = "<table class=bTable cellspacing=0 cellspacing=0 width='100%'>
	<tr>
		<th>Hostname</th>
		<th>Type</th>
		<th>Content</th>
		<th>Add</th>
	</tr>
        <tr>
                <td nowrap valign='middle'><input type='text' name='record_name' id='record_name' maxlength='128'><b> (.)$dom[dns_dom_name]</b></td>
                <td>
			<select name='record_type' id='record_type'>
				<option value='A'>A</option>
				<option value='MX'>MX</option>
			</select>
		</td>
                <td><input type='text' name='record_content' id='record_content' maxlength='128'></td>
                <td>
			<input type='hidden' name='dns_dom_name' value='$dom[dns_dom_name]'>
			<input type='hidden' name='record_domain_id' value='$dns_dom_id'>
			<input type='submit' value='Add' onClick=\"addRecord(fetchForm());\">
		</td>
        </tr>
	<tr>
		<td colspan=4 align='center'>
			An A record should look like this: <b>192.168.1.50</b><br>MX records are literal, e.g. <b>mx.your-mail-server.com</b>.
		</td>
	</tr>

</table>";

?>
<div>
	<br /><b>View / Edit DNS Records</b><br />
	<p align="justify">Your powerful and advanced DNS records management -  now you can view and modify the DNS records for the domain name(s), which you have registered with us as a Private DNS.  You are able to set custom A and MX records and add new hostnames. You may redirect the 'www' to another website of yours hosted elsewhere, thus keeping the Private DNS enabled and operating. These new features will give you more control over the way you combine your own name servers with your custom reseller website.</p>
	<p>
		<table class="selected_dns" width="100%" align="center">
			<tr>
				<td align="justify"><strong style="color: #005AB3";>By default:</strong> Both <?=$dom[dns_dom_name]?> and www.<?=$dom[dns_dom_name]?> should have the default IP address <b>216.65.1.131</b> set as an A record, in order to open the website template of your choice. 
				</td>
			</tr>
			<tr>
				<td align="justify"><strong style="color: #006600";>Tip:</strong> You may redirect <b>www.<?=$dom[dns_dom_name]?></b> to your custom reseller website hosted in an account on SupremeCenterXX. Simply type the shared IP of the SupremeCenterXX's Apache web server as an A record for <?=$dom[dns_dom_name]?> and www.<?=$dom[dns_dom_name]?>.
				</td>
			</tr>
			<tr>
				<td align="justify"><strong style="color: red";>Important!</strong> You should add <?=$dom[dns_dom_name]?> to the Hosted Domains menu in your hosting account on SupremeCenterXX.com. Enable the feature "<b>Don't manage DNS for this domain.</b>" there!
				</td>
			</tr>
			<tr>
				<td align="justify"><strong style="color: #D86400";>More:</strong> If you'd like, you may use another mail server to handle your reseller's correspondence. This is done by changing the MX record for <?=$dom[dns_dom_name]?>. If the MX record is set to the default <b>mail.supremecenter.com</b>, you will be able to use sales@<?=$dom[dns_dom_name]?>. By changing the MX record to <b>mx1/mx2.<?=$dom[dns_dom_name]?></b>, you will be able to receive mail directly in your hosting account on SupremeCenterXX.com.
				</td>
			</tr>
		</table>
	</p>
</div>
<div>
	<? echo $recordsTable;?>		
	<br /><br />
	<form id='sform' name='sform' onSubmit="return false;">
	<? echo $addTable;?>
	</form>
	<br />
	<center><input type="submit" value="Go Back" onClick="LoadPage('managednsDomains', '');var timeoutID = setTimeout('LoadDnsDomainInfo(\'dns_dom_id::<? echo $dns_dom_id; ?>\');',3000);"></center>
</div>